package com.unf.edu.rms.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unf.edu.rms.dao.Faculty;
import com.unf.edu.rms.dao.UserLogin;
import com.unf.edu.rms.dao.UserRole;

@Service
public class UsersService {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public List<UserLogin> viewUserTable()
	{
		System.out.println("UserService.viewCategoryTable()");
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin";
		Query query = session.createQuery(hql);
		List <UserLogin> usersList = query.list();
		for(int i = 0; i < usersList.size(); i++){
			UserLogin userLoginList = (UserLogin) usersList.get(i);
			//System.out.println("StudentService.viewStudentTable()====="+student.getFirstName());
			//System.out.println("StudentService.viewStudentTable()====="+student.getLastName());
			//System.out.println("StudentService.viewStudentTable()====="+student.getNnumber());
		}
		
		return usersList;
	}
	
	@Transactional
	public List<UserRole> viewUserRoleTable()
	{
		System.out.println("UserServer.viewUserRoleTable()");
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserRole";
		Query query = session.createQuery(hql);
		List<UserRole> usersRoleList = query.list();
		for(int i = 0; i < usersRoleList.size(); i++)
		{
			UserRole userRoleList = (UserRole) usersRoleList.get(i);
		}
		return usersRoleList;
	}
	
	@Transactional
	public void addNewUser(UserLogin userLogin)
	{
		Session session = sessionFactory.getCurrentSession();
		session.save(userLogin);
		
	}
	
	@Transactional
	public UserLogin getUserById(int userLoginId)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userLoginId=:userLoginId";
		Query query = session.createQuery(hql);
		query.setInteger("userLoginId", userLoginId);
		UserLogin userLogin = (UserLogin)query.list().get(0);
		return userLogin;
	}
	
	
	@Transactional
	public void updateUser(UserLogin userLogin)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserLogin where userLoginId=:userLoginId";
		Query query = session.createQuery(hql);
		query.setInteger("userLoginId", userLogin.getUserLoginId());
		session.update(userLogin);
	}
}

	